package org.loofer.ext.component.info;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import org.loofer.ext.Ext;
import org.loofer.ext.component.logger.Logger;
import org.loofer.ext.utils.NetWorkUtils;
import io.rong.imlib.statistics.UserData;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

class NetworkDash {
    private static final NetworkObserver OBSERVER = new NetworkObserver() {
        public void onNetworkChanged() {
            NetworkDash.updateNetworkState();
        }
    };
    private static final List<WeakReference<NetworkStateListener>> OBSERVER_LIST = new ArrayList();
    private static final String TAG = "NetworkDash";
    private static NetworkState currState;
    private static ServiceProvider imsiProvider = null;
    private static NetworkState lastState;
    private static Handler mainHandler;

    NetworkDash() {
    }

    static {
        updateNetworkState();
        OBSERVER.startListen();
    }

    public static boolean isAvailable() {
        updateNetworkState();
        NetworkState state = getCurrState();
        return state != null && state.isConnected();
    }

    public static boolean isAvailableFast() {
        NetworkState state = getCurrState();
        return state != null && state.isConnected();
    }

    public static AccessPoint getAccessPoint() {
        NetworkState state = getCurrState();
        if (state != null) {
            return state.getAccessPoint();
        }
        return AccessPoint.NONE;
    }

    public static String getLocalIP() {
        return NetWorkUtils.getLocalIP();
    }

    public static NetworkType getType() {
        NetworkState state = getCurrState();
        if (state != null) {
            return state.getType();
        }
        return NetworkType.NONE;
    }

    public static String getApnName() {
        NetworkState state = getCurrState();
        if (state != null) {
            return state.getApnName();
        }
        return "";
    }

    public static String getApnNameOrWifi() {
        if (!isAvailable()) {
            return "";
        }
        if (isWifi()) {
            return NetWorkUtils.APN_NAME_WIFI;
        }
        return getApnName();
    }

    public static String getApnNameOrWifiOrEthernet() {
        if (!isAvailable()) {
            return "";
        }
        if (isWifi()) {
            return NetWorkUtils.APN_NAME_WIFI;
        }
        if (isEthernet()) {
            return "ethernet";
        }
        return getApnName();
    }

    public static ServiceProvider getProvider() {
        NetworkState state = getCurrState();
        if (state != null) {
            return state.getAccessPoint().getProvider();
        }
        return ServiceProvider.NONE;
    }

    public static ServiceProvider getProvider(boolean useIMSIFirst) {
        ServiceProvider provider = ServiceProvider.NONE;
        if (useIMSIFirst) {
            provider = getIMSIProvider();
            if (!ServiceProvider.NONE.equals(provider)) {
                return provider;
            }
        }
        return getProvider();
    }

    public static ServiceProvider updateIMSIProvider() {
        try {
            ServiceProvider serviceProvider;
            synchronized (NetworkDash.class) {
                String IMSI = getIMSI();
                imsiProvider = ServiceProvider.fromIMSI(IMSI);
                Logger.v(TAG, IMSI + " => " + imsiProvider);
                serviceProvider = imsiProvider;
            }
            return serviceProvider;
        } catch (Exception e) {
            return ServiceProvider.NONE;
        }
    }

    public static String getIMSI() {
        try {
            String IMSI1 = ((TelephonyManager) Ext.getContext().getSystemService(UserData.PHONE_KEY)).getSimOperator();
            if (TextUtils.isEmpty(IMSI1)) {
                IMSI1 = getDeviceIdBySlot(Ext.getContext(), 0);
            }
            if (TextUtils.isEmpty(IMSI1)) {
                IMSI1 = getDeviceIdBySlot(Ext.getContext(), 1);
            }
            Logger.v(TAG, "imsi: " + IMSI1);
            return IMSI1;
        } catch (Exception e) {
            return null;
        }
    }

    public static String getDeviceIdBySlot(Context context, int slotID) {
        String str = null;
        Logger.v(TAG, "isDeviceIdBySlot:" + slotID);
        if (context != null && slotID >= 0 && slotID <= 1) {
            str = null;
            try {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(UserData.PHONE_KEY);
                Object ob_phone = Class.forName("android.telephony.TelephonyManager").getMethod("getSimOperatorGemini", new Class[]{Integer.TYPE}).invoke(telephonyManager, new Object[]{Integer.valueOf(slotID)});
                if (ob_phone != null) {
                    str = ob_phone.toString();
                }
            } catch (Exception e) {
                Logger.w(TAG, "getDeviceIdBySlot", e);
            }
        }
        return str;
    }

    public static ServiceProvider getIMSIProvider() {
        if (imsiProvider == null) {
            updateIMSIProvider();
        }
        return imsiProvider;
    }

    public static boolean isWap() {
        return getAccessPoint().isWap();
    }

    public static boolean isMobile() {
        NetworkType type = getType();
        return NetworkType.MOBILE_4G.equals(type) || NetworkType.MOBILE_3G.equals(type) || NetworkType.MOBILE_2G.equals(type);
    }

    public static boolean is2G() {
        return NetworkType.MOBILE_2G.equals(getType());
    }

    public static boolean is3G() {
        return NetworkType.MOBILE_3G.equals(getType());
    }

    public static boolean isWifi() {
        return NetworkType.WIFI.equals(getType());
    }

    public static boolean isEthernet() {
        return NetworkType.ETHERNET.equals(getType());
    }

    public static int getCellLevel() {
        return OBSERVER.getCellLevel();
    }

    public static void addListener(NetworkStateListener listener) {
        synchronized (OBSERVER_LIST) {
            OBSERVER_LIST.add(new WeakReference(listener));
        }
    }

    public static void removeListener(NetworkStateListener listener) {
        synchronized (OBSERVER_LIST) {
            WeakReference<NetworkStateListener> reference = null;
            for (WeakReference<NetworkStateListener> weakReference : OBSERVER_LIST) {
                NetworkStateListener realListener = (NetworkStateListener) weakReference.get();
                if (realListener != null && realListener.equals(listener)) {
                    reference = weakReference;
                    break;
                }
            }
            OBSERVER_LIST.remove(reference);
        }
    }

    private static void notifyNetworkStateChange() {
        if (OBSERVER_LIST != null) {
            synchronized (OBSERVER_LIST) {
                for (WeakReference<NetworkStateListener> listener : OBSERVER_LIST) {
                    NetworkStateListener realListener = (NetworkStateListener) listener.get();
                    if (realListener != null) {
                        realListener.onNetworkStateChanged(getLastState(), getCurrState());
                    }
                }
            }
        }
    }

    public static boolean updateNetworkState() {
        boolean z;
        synchronized (NetworkDash.class) {
            NetworkInfo info;
            try {
                ConnectivityManager manager = (ConnectivityManager) Ext.getContext().getSystemService("connectivity");
                if (manager == null) {
                    z = false;
                } else {
                    info = manager.getActiveNetworkInfo();
                    z = setCurrState(NetworkState.fromNetworkInfo(info));
                    if (z) {
                        updateIMSIProvider();
                        if (mainHandler == null) {
                            mainHandler = new Handler(Ext.getContext().getMainLooper());
                        }
                        mainHandler.post(new Runnable() {
                            public void run() {
                                NetworkDash.notifyNetworkStateChange();
                            }
                        });
                    }
                }
            } catch (Error e) {
                info = null;
            } catch (Exception e2) {
                info = null;
            }
        }
        return z;
    }

    public static NetworkState getCurrState() {
        return currState;
    }

    protected static NetworkState getLastState() {
        return lastState;
    }

    protected static boolean setCurrState(NetworkState newState) {
        boolean changed;
        synchronized (NetworkDash.class) {
            changed = false;
            if (currState == null) {
                lastState = null;
                currState = newState;
                changed = true;
            } else if (!currState.equals(newState)) {
                lastState = currState;
                currState = newState;
                changed = true;
            }
            if (changed) {
                Logger.v(TAG, "LAST -> " + lastState);
                Logger.v(TAG, "CURR -> " + currState);
            }
        }
        return changed;
    }
}
